//
//  dao.m
//  chilemat
//
//  Created by Rodrigo Hinojosa on 21-02-14.
//  Copyright (c) 2014 Chilemat SpA. All rights reserved.
//

#import "dao.h"

@implementation dao

- (NSString *) obtenerRutaBD{
    NSString *dirDocs;
    NSArray *rutas;
    NSString *rutaBD;
    
    rutas = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    dirDocs = [rutas objectAtIndex:0];
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    rutaBD = [[NSString alloc] initWithString:[dirDocs stringByAppendingPathComponent:@"ecommerce.sqlite"]];
    
    if([fileMgr fileExistsAtPath:rutaBD] == NO){
        [fileMgr copyItemAtPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ecommerce.sqlite"] toPath:rutaBD error:NULL];
    }
    
    return rutaBD;
}

- (void ) deleteCallCenterNumber{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CALL_CENTER"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement 7878.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) insertCallCenterNumber:(NSString*) strPhoneNumber{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_CALL_CENTER(ECC_PHONE_NUMBER) VALUES ('%@')", strPhoneNumber];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_CALL_CENTER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) updateCallCenterNumber:(NSString*) strPhoneNumber{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_CALL_CENTER SET ECC_PHONE_NUMBER = '%@'", strPhoneNumber];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_CALL_CENTER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (NSString *) obtainCallCenterNumber{

	NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSString *strPhoneNumber;
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT ECC_PHONE_NUMBER FROM ECOM_CALL_CENTER";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT ECC_PHONE_NUMBER");
	}
    
    strPhoneNumber = @"0";
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		strPhoneNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return strPhoneNumber;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
 CREATE TABLE "ECOM_MARCAS_PRODUCTOS"
 ("ID_MARCA_PRODUCTO"               INTEGER     PRIMARY KEY NOT NULL,
 "DESCRIPCION_MARCA_PRODUCTO"      TEXT        NOT NULL)
 */

- (void ) deleteMarcaProducto:(int) intIdMarca{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_MARCAS_PRODUCTOS WHERE ID_MARCA_PRODUCTO =%i", intIdMarca];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_MARCAS_PRODUCTOS WHERE ID_MARCA_PRODUCTO = ");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) insertMarcaProducto:(int) intIdMarca
         strDescripcionMarca:(NSString *) strDescripcionMarca{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_MARCAS_PRODUCTOS (ID_MARCA_PRODUCTO, DESCRIPCION_MARCA_PRODUCTO) VALUES(%i,'%@')", intIdMarca, strDescripcionMarca];
        
        NSLog(@"sqlInsert =%@", sqlInsert);
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_MARCAS_PRODUCTOS ");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
 CREATE TABLE "ECOM_DET_CARRO_COMPRA"
 ("DCARR_ID_CARRO"              INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 "USIS_ID"                     INTEGER     NOT NULL,
 "PROD_ID_PRODUCTO"            INTEGER     NOT NULL,
 "DCARR_SKU"                   TEXT        NOT NULL,
 "DCARR_DESCRIPCION_PRODUCTO"  TEXT        NOT NULL,
 "ID_MARCA_PRODUCTO"           INTEGER     NOT NULL,
 "DCARR_CANTIDAD"              FLOAT       NOT NULL,
 "DCARR_NETO"                  INTEGER     NOT NULL,
 "DCARR_IVA"                   INTEGER     NOT NULL,
 "DCARR_TOTAL"                 INTEGER     NOT NULL)
 */
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (int ) insertProductShoppingCart:(int) intIdUsuario
                        intIdCarro:(int) intIdCarro
                     intIdProducto:(int) intIdProducto
                            strSKU:(NSString *) strSKU
                       strDescProd:(NSString *) strDescProd
                        intIdMarca:(int) intIdMarca
                       dblCantidad:(float) dblCantidad
                           intNeto:(int) intNeto
                            intIva:(int) intIva
                          intTotal:(int) intTotal{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_DET_CARRO_COMPRA (DCARR_ID_CARRO, USIS_ID, PROD_ID_PRODUCTO, DCARR_SKU, DCARR_DESCRIPCION_PRODUCTO, ID_MARCA_PRODUCTO, DCARR_CANTIDAD, DCARR_NETO, DCARR_IVA, DCARR_TOTAL) VALUES(%i,%i, %i, '%@', '%@', %i, %.2f, %i, %i, %i)", intIdCarro, intIdUsuario, intIdProducto, strSKU, strDescProd, intIdMarca, dblCantidad, intNeto, intIva, intTotal];
        
        NSLog(@"sqlInsert =%@", sqlInsert);
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO INSERT INTO ECOM_DET_CARRO_COMPRA ");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

- (void ) deleteProductShoppingCart: (int) intIdUsuario intIdProducto:(int) intIdProducto{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_DET_CARRO_COMPRA WHERE PROD_ID_PRODUCTO =%i AND USIS_ID = %i", intIdProducto, intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_DET_CARRO_COMPRA ");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
};

- (void ) deleteProductsShoppingCart: (int) intIdUsuario{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_DET_CARRO_COMPRA WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_DET_CARRO_COMPRA WHERE USIS_ID = ");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
};


- (int) obtainIdShoppingCart:(int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdShoppingCart;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT MAX(DCARR_ID_CARRO) FROM ECOM_DET_CARRO_COMPRA WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT MAX(DCARR_ID_CARRO) FROM ECOM_DET_CARRO_COMPRA WHERE USIS_ID =");
    }
    
    intIdShoppingCart = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdShoppingCart = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdShoppingCart;
}

- (NSMutableArray *) obtainProductsShoppingCart:(int) intIdUsuario {
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayProduct;
    
    NSMutableArray * ArrayProducts;
    
    [ArrayProducts removeAllObjects];
    ArrayProducts = [[NSMutableArray alloc] init];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    //nullif('', ECOM_DET_CARRO_COMPRA.ID_MARCA_PRODUCTO)
    
    //nullif(ECOM_MARCAS_PRODUCTOS.DESCRIPCION_MARCA_PRODUCTO, ECOM_DET_CARRO_COMPRA.ID_MARCA_PRODUCTO)
    
    //nullif('' ,ECOM_MARCAS_PRODUCTOS.DESCRIPCION_MARCA_PRODUCTO)
    
    //IFNull(ECOM_MARCAS_PRODUCTOS.DESCRIPCION_MARCA_PRODUCTO,'t')
    
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID, PROD_ID_PRODUCTO, DCARR_SKU, DCARR_DESCRIPCION_PRODUCTO, ECOM_DET_CARRO_COMPRA.ID_MARCA_PRODUCTO, IFNull(ECOM_MARCAS_PRODUCTOS.DESCRIPCION_MARCA_PRODUCTO,' '), DCARR_CANTIDAD, DCARR_NETO, DCARR_IVA, DCARR_TOTAL FROM ECOM_DET_CARRO_COMPRA left JOIN ECOM_MARCAS_PRODUCTOS ON ECOM_DET_CARRO_COMPRA.ID_MARCA_PRODUCTO = ECOM_MARCAS_PRODUCTOS.ID_MARCA_PRODUCTO WHERE USIS_ID =%i AND PROD_ID_PRODUCTO > 0 ORDER BY DCARR_ID_CARRO", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID, PROD_ID_PRODUCTO, DCARR_SKU, DCARR_DESCRIPCION_PRODUCTO, ECOM_DET_CARRO_COMPRA.ID_MARCA_PRODUCTO, ECOM_MARCAS_PRODUCTOS.DESCRIPCION_MARCA_PRODUCTO, DCARR_CANTIDAD, DCARR_NETO, DCARR_IVA, DCARR_TOTAL FROM ECOM_DET_CARRO_COMPRA JOIN ECOM_MARCAS_PRODUCTOS ON ECOM_DET_CARRO_COMPRA.ID_MARCA_PRODUCTO = ECOM_MARCAS_PRODUCTOS.ID_MARCA_PRODUCTO");
    }
    
    NSString *strIdUsuario, *strIdProducto, *strIdSKU, *strDescripcionProd, *strIdMarca, *strDescripcionMarca, *strCantidad, *strNeto, *strIva, *strTotal;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){

        strIdUsuario = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
        strIdProducto = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
        strIdSKU = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
        strDescripcionProd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,3 )];
        strIdMarca =[NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,4 )];
        strDescripcionMarca = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,5 )];
        strCantidad = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,6 )];
        strNeto =[NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,7 )];
        strIva =[NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,8 )];
        strTotal = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,9 )];
        
        NSLog(@"strIdProducto =!!!%@", strIdProducto);
        
        ArrayProduct = [NSArray arrayWithObjects: strIdUsuario, strIdProducto, strIdSKU, strDescripcionProd, strIdMarca, strDescripcionMarca, strCantidad, strNeto, strIva, strTotal, nil];
        
        [ArrayProducts addObject:ArrayProduct];
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayProducts;
}

- (NSMutableArray *) obtainComunas:(int) intIdCiudad{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayComuna;
    
    NSMutableArray * ArrayComunas;
    
    [ArrayComunas removeAllObjects];
    ArrayComunas = [[NSMutableArray alloc] init];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT COM_ID, COM_DESCRIPCION, CI_ID_CIUDAD FROM ECOM_COMUNAS WHERE CI_ID_CIUDAD =%i", intIdCiudad];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT COM_ID, COM_DESCRIPCION, CI_ID_CIUDAD FROM ECOM_COMUNAS WHERE CI_ID_CIUDAD =");
    }
    
    NSString *strIdComuna, *strDescripcionComuna, *strIdCiudad;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
        strIdComuna = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
        strDescripcionComuna = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
        strIdCiudad = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
        
        ArrayComuna = [NSArray arrayWithObjects: strIdComuna, strDescripcionComuna, strIdCiudad, nil];
        
        [ArrayComunas addObject:ArrayComuna];
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayComunas;
    
}

- (NSArray *) obtainResumeShoppingCart:(int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayTotalShoppingCart;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT  ifnull( SUM(DCARR_TOTAL * DCARR_CANTIDAD) , '0'), ifnull(COUNT(*) , '0') FROM ECOM_DET_CARRO_COMPRA WHERE USIS_ID =%i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT SUM(DCARR_TOTAL) , COUNT(*) FROM ECOM_DET_CARRO_COMPRA");
    }
    
    NSString *strTotal, *strCantidadTotal;
    
    strTotal = @"0";
    strCantidadTotal = @"0";
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
		strTotal = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
		strCantidadTotal = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
    }
    
    ArrayTotalShoppingCart = [NSArray arrayWithObjects: strTotal, strCantidadTotal, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayTotalShoppingCart;
}

- (int ) obtainExistProductShoppingCart:(int) intIdUsuario intIdProducto:(int) intIdProducto{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdCurrentUser;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_DET_CARRO_COMPRA WHERE USIS_ID = %i AND PROD_ID_PRODUCTO =%i", intIdUsuario, intIdProducto];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_DET_CARRO_COMPRA");
    }
    
    intIdCurrentUser = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdCurrentUser = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdCurrentUser;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
 CREATE TABLE "ECOM_PRODUCTOS" (
 "PRO_ID_PRODUCTO"              INTEGER PRIMARY KEY NOT NULL,
 "PRO_DESCRIPCION_PRODUCTO"     TEXT NOT NULL,
 "PRO_DESCRIPCION_COMPLENTARIA" TEXT NULL,
 "PRO_OVERVIEW"                 TEXT NULL,
 "PRO_SKU"                      TEXT NOT NULL,
 "PRO_REVIEWS"                  INTEGER NULL,
 "PRO_NRO_VALORACIONES"         INTEGER NULL,
 "PRO_PRECIO"                   INTEGER NOT NULL,
 "PRO_PRECIO_OFERTA"            INTEGER NULL,
 "PRO_DESCUENTO"                INTEGER NULL,
 "PRO_TIEMPO_OFERTA"            TEXT NULL,
 "USIS_ID"                      INTEGER NOT NULL
 "ID_MARCA_PRODUCTO"            INTEGER,
 "DESCRIPCION_MARCA_PRODUCTO"   INTEGER)
 

- (int ) insertProduct:(int) intIdUsuario                           //Ok
         intIdProducto:(int) intIdProducto                          //Ok
           strDescProd:(NSString *) strDescProd                     //Ok
 strDescComplementaria:(NSString *) strDescComplementaria           //Ok
           strOverview:(NSString *) strOverview                     //Ok
                strSKU:(NSString *) strSKU                          //Ok
         intNroReviews:(int) intNroReviews                          //Ok
    intNroValoraciones:(int) intNroValoraciones                     //NO
             intPrecio:(int) intPrecio                              //Ok
       intPrecioOferta:(int) intPrecioOferta                        //Ok
          intDescuento:(int) intDescuento                           //Ok
       strTiempoOferta:(NSString *) strTiempoOferta;                //OK

- (void ) deleteProduct:(int) intIdUsuario
          intIdProducto:(int) intIdProducto;

- (void ) deleteProducts:(int) intIdUsuario;

- (NSArray *) obtainProduct:(int) intIdUsuario
              intIdProducto:(int) intIdProducto;
 
- (NSArray *) obtainProducts:(int) intIdUsuario;
*/

- (int ) insertProduct:(int) intIdUsuario                           //Ok
         intIdProducto:(int) intIdProducto                          //Ok
           strDescProd:(NSString *) strDescProd                     //Ok
 strDescComplementaria:(NSString *) strDescComplementaria           //Ok
           strOverview:(NSString *) strOverview                     //Ok
                strSKU:(NSString *) strSKU                          //Ok
         intNroReviews:(int) intNroReviews                          //Ok
    intNroValoraciones:(int) intNroValoraciones                     //NO
             intPrecio:(int) intPrecio                              //Ok
       intPrecioOferta:(int) intPrecioOferta                        //Ok
          intDescuento:(int) intDescuento                           //Ok
       strTiempoOferta:(NSString *) strTiempoOferta                 //Ok
            intIdMarca:(int) intIdMarca                             //Ok
   strDescripcionMarca:(NSString *) strDescripcionMarca{            //Ok
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_PRODUCTOS (PRO_ID_PRODUCTO, PRO_DESCRIPCION_PRODUCTO, PRO_DESCRIPCION_COMPLENTARIA, PRO_OVERVIEW, PRO_SKU, PRO_REVIEWS, PRO_NRO_VALORACIONES, PRO_PRECIO, PRO_PRECIO_OFERTA, PRO_DESCUENTO, PRO_TIEMPO_OFERTA, USIS_ID, ID_MARCA_PRODUCTO, DESCRIPCION_MARCA_PRODUCTO) VALUES(%i, '%@', '%@', '%@', '%@', %i, %i, %i, %i, %i, '%@', %i, %i, '%@')", intIdProducto, strDescProd, strDescComplementaria, strOverview, strSKU, intNroReviews, intNroValoraciones, intPrecio, intPrecioOferta, intDescuento, strTiempoOferta, intIdUsuario, intIdMarca, strDescripcionMarca];

        NSLog(@"sqlInsert =%@", sqlInsert);
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_PRODUCTOS");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

- (void ) deleteProduct: (int) intIdUsuario intIdProducto:(int) intIdProducto{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_PRODUCTOS WHERE PROD_ID_PRODUCTO =%i AND USIS_ID = %i", intIdUsuario, intIdProducto];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_PRODUCTOS");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
};

- (void ) deleteProducts: (int) intIdUsuario {
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_PRODUCTOS WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_PRODUCTOS WHERE USIS_ID = ");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
};

- (void ) deleteProductsAll{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_PRODUCTOS"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_PRODUCTOS");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (NSArray *) obtainProduct:(int) intIdUsuario intIdProducto:(int) intIdProducto{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayProduct;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT PRO_ID_PRODUCTO, PRO_DESCRIPCION_PRODUCTO, PRO_DESCRIPCION_COMPLENTARIA, PRO_OVERVIEW, PRO_SKU, PRO_REVIEWS, PRO_NRO_VALORACIONES, PRO_PRECIO, PRO_PRECIO_OFERTA, PRO_DESCUENTO, PRO_TIEMPO_OFERTA, ID_MARCA_PRODUCTO, DESCRIPCION_MARCA_PRODUCTO FROM ECOM_PRODUCTOS WHERE USIS_ID =%i AND PRO_ID_PRODUCTO =%i", intIdUsuario, intIdProducto];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT PRO_ID_PRODUCTO, PRO_DESCRIPCION_PRODUCTO, PRO_DESCRIPCION_COMPLENTARIA, PRO_OVERVIEW, PRO_SKU, PRO_REVIEWS, PRO_NRO_VALORACIONES, PRO_PRECIO, PRO_PRECIO_OFERTA, PRO_DESCUENTO, PRO_TIEMPO_OFERTA, ID_MARCA_PRODUCTO, DESCRIPCION_MARCA_PRODUCTO FROM ECOM_PRODUCTOS ");
    }
    
    NSString *strIdProducto, *strDescProd, *strDescComplementaria, *strOverview, *strSKU, *strNroReviews, *strNroValoraciones, *strPrecio, *strPrecioOferta, *strDescuento, *strTiempoOferta, *strIdMarca, *strDescripcionMarca;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
		strIdProducto = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
		strDescProd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
		strDescComplementaria = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
		strOverview = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,3 )];
		strSKU = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,4 )];
		strNroReviews = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,5 )];
		strNroValoraciones = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,6 )];
		strPrecio = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,7 )];
		strPrecioOferta = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,8 )];
		strDescuento = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,9 )];
		strTiempoOferta = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,10 )];

		strIdMarca = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,11 )];
		strDescripcionMarca = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,12 )];
    }
    
    ArrayProduct = [NSArray arrayWithObjects: strIdProducto, strDescProd, strDescComplementaria, strOverview, strSKU, strNroReviews, strNroValoraciones, strPrecio, strPrecioOferta, strDescuento,      strTiempoOferta, strIdMarca, strDescripcionMarca, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayProduct;
}

- (NSMutableArray *) obtainProducts:(int) intIdUsuario {
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayProduct;

    NSMutableArray * ArrayProducts;

    [ArrayProducts removeAllObjects];
    ArrayProducts = [[NSMutableArray alloc] init];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT PRO_ID_PRODUCTO, PRO_DESCRIPCION_PRODUCTO, PRO_DESCRIPCION_COMPLENTARIA, PRO_OVERVIEW, PRO_SKU, PRO_REVIEWS, PRO_NRO_VALORACIONES, PRO_PRECIO, PRO_PRECIO_OFERTA, PRO_DESCUENTO, PRO_TIEMPO_OFERTA, ID_MARCA_PRODUCTO, DESCRIPCION_MARCA_PRODUCTO FROM ECOM_PRODUCTOS WHERE USIS_ID =%i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT PRO_ID_PRODUCTO, PRO_DESCRIPCION_PRODUCTO, PRO_DESCRIPCION_COMPLENTARIA, PRO_OVERVIEW, PRO_SKU, PRO_REVIEWS, PRO_NRO_VALORACIONES, PRO_PRECIO, PRO_PRECIO_OFERTA, PRO_DESCUENTO, PRO_TIEMPO_OFERTA, ID_MARCA_PRODUCTO, DESCRIPCION_MARCA_PRODUCTO FROM ECOM_PRODUCTOS ");
    }
    
    NSString *strIdProducto, *strDescProd, *strDescComplementaria, *strOverview, *strSKU, *strNroReviews, *strNroValoraciones, *strPrecio, *strPrecioOferta, *strDescuento, *strTiempoOferta, *strIdMarca, *strDescripcionMarca;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
		strIdProducto = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
		strDescProd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
		strDescComplementaria = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
		strOverview = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,3 )];
		strSKU = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,4 )];
		strNroReviews = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,5 )];
		strNroValoraciones = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,6 )];
		strPrecio = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,7 )];
		strPrecioOferta = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,8 )];
		strDescuento = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,9 )];
		strTiempoOferta = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,10 )];
		strIdMarca = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,11 )];
		strDescripcionMarca = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,12 )];

        NSLog(@"strIdProducto =!!!%@", strIdProducto);
        
        ArrayProduct = [NSArray arrayWithObjects: strIdProducto, strDescProd, strDescComplementaria, strOverview, strSKU, strNroReviews, strNroValoraciones, strPrecio, strPrecioOferta, strDescuento, strTiempoOferta, strIdMarca, strDescripcionMarca, nil];

        [ArrayProducts addObject:ArrayProduct];
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayProducts;
}


/*
 CREATE TABLE "ECOM_DET_CARRO_COMPRA" (
 "DCARR_ID_CARRO"               INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 "USIS_ID"                      INTEGER NOT NULL,
 "PROD_ID_PRODUCTO"             INTEGER NOT NULL,
 "DCARR_SKU"                    TEXT NOT NULL,
 "DCARR_DESCRIPCION_PRODUCTO"   TEXT NOT NULL,
 "ID_MARCA_PRODUCTO"            INTEGER NOT NULL,
 "DCARR_CANTIDAD"               FLOAT NOT NULL,
 "DCARR_NETO"                   INTEGER NOT NULL,
 "DCARR_IVA"                    INTEGER NOT NULL,
 "DCARR_TOTAL"                  INTEGER NOT NULL)

  DCARR_ID_CARRO, USIS_ID, PROD_ID_PRODUCTO, DCARR_SKU, DCARR_DESCRIPCION_PRODUCTO, ID_MARCA_PRODUCTO, DCARR_CANTIDAD, DCARR_NETO, DCARR_IVA, DCARR_TOTAL
*/

- (int ) updateProductShoppingCart:(int) intIdUsuario
              intIdProducto:(int) intIdProducto
                     strSKU:(NSString *) strSKU
                strDescProd:(NSString *) strDescProd
                 intIdMarca:(int) intIdMarca
                dblCantidad:(float) dblCantidad
                    intNeto:(int) intNeto
                     intIVA:(int) intIVA
                   intTotal:(int) intTotal{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_DET_CARRO_COMPRA SET DCARR_SKU ='%@', DCARR_DESCRIPCION_PRODUCTO ='%@', ID_MARCA_PRODUCTO =%i, DCARR_CANTIDAD =%.2f, DCARR_NETO =%i, DCARR_IVA =%i, DCARR_TOTAL =%i WHERE PROD_ID_PRODUCTO =%i AND USIS_ID = %i"
                               , strSKU, strDescProd, intIdMarca, dblCantidad, intNeto, intIVA, intTotal, intIdProducto, intIdUsuario];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement PDATE ECOM_DET_CARRO_COMPRA SET DCARR_SKU =");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

- (int ) updateUserShoppingCart:(int) intIdUsuario
                intIdUsuarioNew:(int) intIdUsuarioNew{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_DET_CARRO_COMPRA SET USIS_ID =%i WHERE USIS_ID = %i", intIdUsuarioNew, intIdUsuario];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_DET_CARRO_COMPRA SET USIS_ID =");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

- (int ) updateProductQtyShoppingCart:(int) intIdUsuario
                     intIdProducto:(int) intIdProducto
                       dblCantidad:(float) dblCantidad{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_DET_CARRO_COMPRA SET DCARR_CANTIDAD =%.2f WHERE PROD_ID_PRODUCTO =%i AND USIS_ID = %i", dblCantidad, intIdProducto, intIdUsuario];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_DET_CARRO_COMPRA SET DCARR_CANTIDAD =");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}


//------------------------------------------------------------------------------------------------
/*
 CREATE TABLE "ECOM_DIRECCIONES_CLIENTE" (
 "DCLI_ID_DIRECCION"     INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 "MC_RUT_CLIENTE"        INTEGER NOT NULL,
 "DCLI_DIRECCION"        TEXT NOT NULL,
 "DCLI_NUMERO"           TEXT NOT NULL,
 "DCLI_REFERENCIA"       TEXT,
 "USIS_ID"               INTEGER,
 "COM_ID"                INTEGER)
*/

- (int ) insertDireccionCliente:(int) intIdUsuario
                 intIdDireccion:(int) intIdDireccion
                  intRutCliente:(int) intRutCliente
                   strDireccion:(NSString *) strDireccion
                      strNumero:(NSString *) strNumero
                  strReferencia:(NSString *) strReferencia
                    intIdComuna:(int) intIdComuna{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_DIRECCIONES_CLIENTE (DCLI_ID_DIRECCION, MC_RUT_CLIENTE, DCLI_DIRECCION, DCLI_NUMERO, DCLI_REFERENCIA, USIS_ID, COM_ID) VALUES(%i, %i, '%@', '%@', '%@', %i, %i)", intIdDireccion, intRutCliente, strDireccion, strNumero, strReferencia, intIdUsuario, intIdComuna];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_DIRECCIONES_CLIENTE");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

- (void) deleteDireccionCliente:(int) intIdUsuario{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_DIRECCIONES_CLIENTE WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_DIRECCIONES_CLIENTE WHERE USIS_ID =");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

/*
 CREATE TABLE "ECOM_DIRECCIONES_CLIENTE" (
 "DCLI_ID_DIRECCION"     INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 "MC_RUT_CLIENTE"        INTEGER NOT NULL,
 "DCLI_DIRECCION"        TEXT NOT NULL,
 "DCLI_NUMERO"           TEXT NOT NULL,
 "DCLI_REFERENCIA"       TEXT,
 "USIS_ID"               INTEGER,
 "COM_ID"                INTEGER)
 */

- (NSArray *) obtainDireccionCliente:(int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayDireccion;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT MC_RUT_CLIENTE, DCLI_DIRECCION, DCLI_NUMERO, DCLI_REFERENCIA, USIS_ID, ECOM_COMUNAS.COM_ID, ECOM_COMUNAS.COM_DESCRIPCION, DCLI_ID_DIRECCION FROM ECOM_DIRECCIONES_CLIENTE JOIN ECOM_COMUNAS WHERE ECOM_DIRECCIONES_CLIENTE.COM_ID = ECOM_COMUNAS.COM_ID AND ECOM_DIRECCIONES_CLIENTE.USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT MC_RUT_CLIENTE, DCLI_DIRECCION, DCLI_NUMERO, DCLI_REFERENCIA, USIS_ID, ECOM_COMUNAS.COM_ID, ECOM_COMUNAS.COM_DESCRIPCION FROM ECOM_DIRECCIONES_CLIENTE JOIN ECOM_COMUNAS");
    }
    
    NSString *strRut, *strDireccion, *strNumero, *strReferencia, *strIdUsuario, *strIdComuna, *strDescripcionComuna, *strIdDireccionCliente;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
        strRut = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
        strDireccion = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
        strNumero = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
        strReferencia = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,3 )];
        strIdUsuario = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,4 )];
        strIdComuna = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,5 )];
        strDescripcionComuna = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,6 )];
        strIdDireccionCliente = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,7 )];
    }
    
    ArrayDireccion = [NSArray arrayWithObjects: strRut, strDireccion, strNumero, strReferencia, strIdUsuario, strIdComuna, strDescripcionComuna , strIdDireccionCliente, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayDireccion;
    
}

/*
 CREATE TABLE "ECOM_MAESTRO_CLIENTES"
("MC_RUT_CLIENTE"      INTEGER,
 "USIS_ID"             INTEGER,
 "COM_ID"              INTEGER,

 "MC_DIGITO"           TEXT,
 "MC_NOMBRE_COMPLETO"  TEXT,
 "MC_APELLIDOS"        TEXT,
 "MC_COD_TELEFONO"     TEXT,
 "MC_TELEFONO"         TEXT,
 
 "MC_DIA"              INTEGER,
 "MC_MES"              INTEGER,
 "MC_ANO"              INTEGER,
 
 "MC_SEXO"             TEXT,
 
 "MCLI_FORMA_PAGO"     INTEGER)
 */

- (int ) insertMaestroClientes:(int) intRutCliente
                  intIdUsuario:(int) intIdUsuario
                     strDigito:(NSString *) strDigito
             strNombreCompleto:(NSString *) strNombreCompleto
                  strApellidos:(NSString *) strApellidos
                strCodTelefono:(NSString *) strCodTelefono
                   strTelefono:(NSString *) strTelefono
                        intDia:(int) intDia
                        intMes:(int) intMes
                        intAno:(int) intAno
                        strSex:(NSString *) strSex
                  intFormaPago:(int) intFormaPago
                      strEmail:(NSString *) strEmail{
   
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_MAESTRO_CLIENTES (MC_RUT_CLIENTE, USIS_ID, MC_DIGITO, MC_NOMBRE_COMPLETO, MC_APELLIDOS, MC_COD_TELEFONO, MC_TELEFONO, MC_DIA, MC_MES, MC_ANO, MC_SEXO, MCLI_FORMA_PAGO, MC_EMAIL) VALUES(%i, %i, '%@', '%@', '%@', '%@', '%@', %i, %i, %i, '%@', %i, '%@')"
            , intRutCliente, intIdUsuario, strDigito, strNombreCompleto, strApellidos, strCodTelefono, strTelefono, intDia, intMes, intAno, strSex, intFormaPago, strEmail];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_REGISTERED");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
}

- (int ) upateMaestroClientes:(int) intRutCliente
                 intIdUsuario:(int) intIdUsuario
                    strDigito:(NSString *) strDigito
            strNombreCompleto:(NSString *) strNombreCompleto
                 strApellidos:(NSString *) strApellidos
               strCodTelefono:(NSString *) strCodTelefono
                  strTelefono:(NSString *) strTelefono
                       intDia:(int) intDia
                       intMes:(int) intMes
                       intAno:(int) intAno
                       strSex:(NSString *) strSex
                 intFormaPago:(int) intFormaPago
                     strEmail:(NSString *) strEmail{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_MAESTRO_CLIENTES SET MC_RUT_CLIENTE = %i, MC_DIGITO = '%@', MC_NOMBRE_COMPLETO = '%@', MC_APELLIDOS = '%@', MC_COD_TELEFONO = '%@', MC_TELEFONO = '%@', MC_DIA = %i, MC_MES = %i, MC_ANO = %i, MC_SEXO = '%@', MCLI_FORMA_PAGO = %i , MC_EMAIL = '%@' WHERE USIS_ID = %i"
                               , intRutCliente, strDigito, strNombreCompleto, strApellidos, strCodTelefono, strTelefono, intDia, intMes, intAno, strSex, intFormaPago, strEmail, intIdUsuario];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_MAESTRO_CLIENTES");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
};

- (void ) deleteMaestroClientes: (int) intIdUsuario{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_MAESTRO_CLIENTES WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_MAESTRO_CLIENTES.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
};

- (int ) obtainMaestroClientes: (int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdUsrRegistered;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_MAESTRO_CLIENTES WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_MAESTRO_CLIENTES WHERE USIS_ID = ");
    }
    
    intIdUsrRegistered = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdUsrRegistered = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdUsrRegistered;
};

- (NSArray *) obtainDatosCliente:(int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayCliente;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT MC_RUT_CLIENTE, MC_DIGITO, MC_NOMBRE_COMPLETO, MC_APELLIDOS, MC_COD_TELEFONO, MC_TELEFONO, MC_DIA, MC_MES, MC_ANO, MC_SEXO, MCLI_FORMA_PAGO, MC_EMAIL FROM ECOM_MAESTRO_CLIENTES WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT MC_RUT_CLIENTE, MC_DIGITO, MC_NOMBRE_COMPLETO, MC_APELLIDOS, MC_COD_TELEFONO, MC_TELEFONO, MC_DIA, MC_MES, MC_ANO, MC_SEXO, MCLI_FORMA_PAGO FROM ECOM_MAESTRO_CLIENTES");
    }
    
    NSString *strRut, *strDigito, *strNombreCompleto, *strApellidos, *strCodTelefono, *strTelefono, *strDia, *strMes, *strAno, *strSexo, *strFormaPago, *strEmail;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
        strRut = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
        strDigito = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,3 )];
        strNombreCompleto = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,4 )];
        strApellidos = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,5 )];
        strCodTelefono = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,6 )];
        strTelefono = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,7 )];
        strDia = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,8 )];
        strMes = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,9 )];
        strAno = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,10 )];
        strSexo = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,11 )];
        strFormaPago = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,12 )];
        strEmail = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,12 )];
    }
    
    ArrayCliente = [NSArray arrayWithObjects: strRut, strDigito, strNombreCompleto, strApellidos, strCodTelefono, strTelefono, strDia, strMes, strAno, strSexo, strFormaPago, strEmail, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayCliente;
    
}

//-------------------------------------------------------------------------------------------------
/*
 CREATE TABLE "ECOM_CLIENT_ACTION" ("CLIENTACT_ID" TEXT)
 */

- (void ) insertClientAction: (NSString *) strAction{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_CLIENT_ACTION (CLIENTACT_ID) VALUES('%@')", strAction];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_CLIENT_ACTION (CLIENTACT_ID)");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteClientAction{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CLIENT_ACTION"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_CLIENT_ACTION.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (NSString *) obtainClientAction{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT CLIENTACT_ID FROM ECOM_CLIENT_ACTION"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT CLIENTACT_ID FROM ECOM_CLIENT_ACTION");
    }
    
    NSString *strClientAction = @"";
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        strClientAction = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return strClientAction;
}



/*
//CREATE TABLE "ECOM_USUARIO_FILTRO_PRECIO" ("USIS_ID" INTEGER PRIMARY KEY NOT NULL, "FPRE_DESDE" INTEGER NOT NULL,"FPRE_HASTA" INTEGER NOT NULL, "FPRE_CURRENT" INTEGER)
 
 - (void ) insertUserFilterPrice: (int) intIdUsuario intPriceDesde: (int) intPriceDesde intPriceHasta: (int) intPriceHasta intPriceCurrent: (int) intPriceCurrent;
 
 - (void ) deleteUserFilterPrice: (int) intIdUsuario;
 
 - (int ) obtainUserFilterPrice:  (int) intIdUsuario;
*/

 - (void ) insertUserFilterPrice: (int) intIdUsuario intPriceDesde: (int) intPriceDesde intPriceHasta: (int) intPriceHasta intPriceCurrent: (int) intPriceCurrent{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_USUARIO_FILTRO_PRECIO (USIS_ID, FPRE_DESDE, FPRE_HASTA, FPRE_CURRENT) VALUES(%i,%i,%i,%i)", intIdUsuario, intPriceDesde, intPriceHasta, intPriceCurrent];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_USUARIO_FILTRO_PRECIO");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) updateUserFilterPrice: (int) intIdUsuario intPriceDesde: (int) intPriceDesde intPriceHasta: (int) intPriceHasta intPriceCurrent: (int) intPriceCurrent{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_USUARIO_FILTRO_PRECIO SET FPRE_DESDE = %i, FPRE_HASTA = %i, FPRE_CURRENT = %i WHERE USIS_ID = %i", intPriceDesde, intPriceHasta, intPriceCurrent, intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_USUARIO_FILTRO_PRECIO");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteUserFilterPrice: (int) intIdUsuario {
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_USUARIO_FILTRO_PRECIO WHERE USIS_ID =%i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_USUARIO_FILTRO_PRECIO.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (NSArray *) obtainUserFilterPrice:  (int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayFilterPrice;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT FPRE_DESDE, FPRE_HASTA, FPRE_CURRENT FROM ECOM_USUARIO_FILTRO_PRECIO WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_MAESTRO_CLIENTES WHERE USIS_ID = ");
    }
    
    NSString *strPriceDesde, *strPriceHasta, *strPriceCurrent;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){

		strPriceDesde = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
		strPriceHasta = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
		strPriceCurrent = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
    }

    ArrayFilterPrice = [NSArray arrayWithObjects: strPriceDesde, strPriceHasta, strPriceCurrent, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayFilterPrice;
}

- (int) obtainExistUserFilterPrice:  (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdCurrentUser;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_USUARIO_FILTRO_PRECIO WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_USUARIO_FILTRO_PRECIO");
    }
    
    intIdCurrentUser = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdCurrentUser = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdCurrentUser;
}

/*
 //CREATE TABLE "ECOM_USUARIO_TIPO_ORDENAMIENTO" ("USIS_ID" INTEGER PRIMARY KEY NOT NULL, "IDTO_ID_TIPO_ORDENAMIENTO" INTEGER NOT NULL, "ID_ROW" INTEGER, "ID_SECTION" INTEGER)
 
 - (void ) insertUserTipoOrdenamiento:(int) intIdUsuario  intIdTipoOrdenamiento: (int) intIdTipoOrdenamiento;
 
 - (void ) updateUserTipoOrdenamiento:(int) intIdUsuario intIdTipoOrdenamiento: (int) intIdTipoOrdenamiento;
 
 - (void ) deleteUserTipoOrdenamiento:(int) intIdUsuario;
 
 - (int ) obtainUserTipoOrdenamiento:(int) intIdUsuario;
*/

- (void ) insertUserTipoOrdenamiento:(int) intIdUsuario  intIdTipoOrdenamiento: (int) intIdTipoOrdenamiento intIdRow: (int) intIdRow intIdSection: (int) intIdSection{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_USUARIO_TIPO_ORDENAMIENTO (USIS_ID, IDTO_ID_TIPO_ORDENAMIENTO, ID_ROW, ID_SECTION) VALUES(%i,%i,%i,%i)", intIdUsuario, intIdTipoOrdenamiento, intIdRow, intIdSection];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_USUARIO_TIPO_ORDENAMIENTO");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) updateUserTipoOrdenamiento:(int) intIdUsuario intIdTipoOrdenamiento: (int) intIdTipoOrdenamiento intIdRow: (int) intIdRow intIdSection: (int) intIdSection{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_USUARIO_TIPO_ORDENAMIENTO SET IDTO_ID_TIPO_ORDENAMIENTO = %i, ID_ROW =%i, ID_SECTION =%i WHERE USIS_ID = %i", intIdTipoOrdenamiento, intIdRow, intIdSection, intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_USUARIO_TIPO_ORDENAMIENTO");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteUserTipoOrdenamiento: (int) intIdUsuario {
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_USUARIO_TIPO_ORDENAMIENTO WHERE USIS_ID =%i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_USUARIO_TIPO_ORDENAMIENTO.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int) obtainUserTipoOrdenamiento:  (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdCurrentUser;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT IDTO_ID_TIPO_ORDENAMIENTO FROM ECOM_USUARIO_TIPO_ORDENAMIENTO WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT IDTO_ID_TIPO_ORDENAMIENTO FROM ECOM_USUARIO_TIPO_ORDENAMIENTO");
    }
    
    intIdCurrentUser = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdCurrentUser = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdCurrentUser;
}

- (NSArray *) obtainUserTipoOrdenamientoData:  (int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayTipoOrdenamiento;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT IDTO_ID_TIPO_ORDENAMIENTO, ID_ROW, ID_SECTION FROM ECOM_USUARIO_TIPO_ORDENAMIENTO WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT IDTO_ID_TIPO_ORDENAMIENTO, ID_ROW, ID_SECTION FROM ECOM_USUARIO_TIPO_ORDENAMIENTO");
    }
    
    NSString *strTipoOrd, *strIdRow, *strIdSection;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
		strTipoOrd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
		strIdRow = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
		strIdSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
    }
    
    ArrayTipoOrdenamiento = [NSArray arrayWithObjects: strTipoOrd, strIdRow, strIdSection, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayTipoOrdenamiento;

}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------
//CREATE TABLE "ECOM_USUARIO_FILTRO_MARCA" ("USIS_ID" INTEGER PRIMARY KEY NOT NULL,"ID_MARCA_PRODUCTO" INTEGER NOT NULL, "ID_ROW" INTEGER, "ID_SECTION" INTEGER)
/*

- (void ) insertUserFilterMarc:(int) intIdUsuario  intIdMarc: (int) intIdMarc intIdRow: (int) intIdRow intIdSection: (int) intIdSection;
 
- (void ) updateUserFilterMarc:(int) intIdUsuario intIdMarc: (int) intIdMarc intIdRow: (int) intIdRow intIdSection: (int) intIdSection;

- (void ) deleteUserFilterMarc;

- (int ) obtainUserFilterMarc;

- (NSArray *) obtainUserFilterMarcData:  (int) intIdUsuario;
 
*/
//------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (NSArray *) obtainUserFilterMarcData:  (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayFilterMarc;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT ID_MARCA_PRODUCTO, ID_ROW, ID_SECTION FROM ECOM_USUARIO_FILTRO_MARCA WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT ID_MARCA_PRODUCTO, ID_ROW, ID_SECTION FROM ECOM_USUARIO_FILTRO_MARCA ");
    }
    
    NSString *strIdMarca, *strIdRow, *strIdSection;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
        strIdMarca = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
        strIdRow = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
        strIdSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
    }
    
    ArrayFilterMarc = [NSArray arrayWithObjects: strIdMarca, strIdRow, strIdSection, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayFilterMarc;
}

- (void ) insertUserFilterMarc:(int) intIdUsuario  intIdMarc: (int) intIdMarc intIdRow: (int) intIdRow intIdSection: (int) intIdSection{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_USUARIO_FILTRO_MARCA (USIS_ID, ID_MARCA_PRODUCTO, ID_ROW, ID_SECTION) VALUES(%i,%i,%i,%i)", intIdUsuario, intIdMarc, intIdRow, intIdSection];

        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_USUARIO_FILTRO_MARCA");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) updateUserFilterMarc:(int) intIdUsuario intIdMarc: (int) intIdMarc intIdRow: (int) intIdRow intIdSection: (int) intIdSection{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_USUARIO_FILTRO_MARCA SET ID_MARCA_PRODUCTO = %i, ID_ROW = %i, ID_SECTION = %i WHERE USIS_ID = %i", intIdMarc, intIdUsuario, intIdRow, intIdSection];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_USUARIO_FILTRO_MARCA SET ID_MARCA_PRODUCTO =");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }

}

- (void ) deleteUserFilterMarc: (int) intIdUsuario {
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_USUARIO_FILTRO_MARCA WHERE USIS_ID =%i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_USUARIO_FILTRO_MARCA");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainUserFilterMarc: (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdMarc;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT ID_MARCA_PRODUCTO FROM ECOM_USUARIO_FILTRO_MARCA"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT ID_MARCA_PRODUCTO FROM ECOM_USUARIO_FILTRO_MARCA");
    }
    
    intIdMarc = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdMarc = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdMarc;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//CREATE TABLE "ECOM_CURRENT_GRID_ORDER" ("GRID_ORDER_ID" INTEGER PRIMARY KEY NOT NULL);
/*
 - (void ) insertCurrentGridOrder: (int) intIdProducto;
 
 - (void ) deleteCurrentGridOrder;
 
 - (int ) obtainCurrentGridOrder;
*/

- (void ) insertCurrentGridOrder: (int) intIdOrder{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_CURRENT_GRID_ORDER (GRID_ORDER_ID) VALUES(%i)", intIdOrder];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_CURRENT_GRID_ORDER (GRID_ORDER_ID)");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteCurrentGridOrder{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CURRENT_GRID_ORDER"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_CURRENT_GRID_ORDER.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainCurrentGridOrder{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdOrder;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT GRID_ORDER_ID FROM ECOM_CURRENT_GRID_ORDER"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT GRID_ORDER_ID FROM ECOM_CURRENT_GRID_ORDER");
    }
    
    intIdOrder = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdOrder = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdOrder;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
 CREATE TABLE "ECOM_IMPUESTO" (
 "IM_ID_IMPUESTO" INTEGER PRIMARY KEY NOT NULL,
 "IM_DESCRIPCION" TEXT NULL,
 "IM_VALOR" FLOAT NOT NULL)
*/

- (int) obtainCurrentTax{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdImpuesto;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT IM_VALOR FROM ECOM_IMPUESTO"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT IM_VALOR FROM ECOM_IMPUESTO");
    }
    
    intIdImpuesto = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdImpuesto = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdImpuesto;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//CREATE TABLE "ECOM_CURRENT_PRODUCT" ("PROD_ID_PRODUCTO" INTEGER PRIMARY KEY NOT NULL)
/*
 - (void ) insertCurrentProduct: (int) intIdProducto;
 
 - (void ) deleteCurrentProduct;
 
 - (int ) obtainCurrentProduct;
 
*/
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 - (void ) insertCurrentProduct: (int) intIdProducto{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_CURRENT_PRODUCT (PROD_ID_PRODUCTO) VALUES(%i)", intIdProducto];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_CURRENT_PRODUCT");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteCurrentProduct{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CURRENT_PRODUCT"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_CURRENT_PRODUCT.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainCurrentProduct{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdCurrentProduct;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT PROD_ID_PRODUCTO FROM ECOM_CURRENT_PRODUCT"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT PROD_ID_PRODUCTO FROM ECOM_CURRENT_PRODUCT");
    }
    
    intIdCurrentProduct = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdCurrentProduct = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdCurrentProduct;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//CREATE TABLE "ECOM_CURRENT_USER" ("USIS_ID" INTEGER PRIMARY KEY NOT NULL)

- (void ) insertCurrentIdUser: (int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_CURRENT_USER (USIS_ID) VALUES(%i)", intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_CURRENT_USER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteCurrentIdUser{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CURRENT_USER"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_CURRENT_USER.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainCurrentIdUser{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdCurrentUser;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_CURRENT_USER"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_CURRENT_USER");
    }
    
    intIdCurrentUser = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdCurrentUser = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdCurrentUser;
}

//------------------------------------------------------------------------------------------------
//CREATE TABLE "ECOM_LOADING_PROCESS" ("LP_ID_LOADING_PROCESS" INTEGER PRIMARY KEY NOT NULL)

- (void ) insertLoading{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_LOADING_PROCESS (LP_ID_LOADING_PROCESS) VALUES(1)"];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_LOADING_PROCESS");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteLoading{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_LOADING_PROCESS"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_LOADING_PROCESS.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
    
}

- (int ) obtainLoading{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdCurrentUser;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT LP_ID_LOADING_PROCESS FROM ECOM_LOADING_PROCESS"];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT LP_ID_LOADING_PROCESS FROM ECOM_LOADING_PROCESS");
    }
    
    intIdCurrentUser = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdCurrentUser = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdCurrentUser;
}

//------------------------------------------------------------------------------------------------
// CREATE TABLE "ECOM_USUARIOS_SISTEMAS" ("USIS_ID" INTEGER PRIMARY KEY NOT NULL, "USIS_US_USUARIO" TEXT NOT NULL, "USIS_US_CLAVE" TEXT NOT NULL, "USIS_ID_SOCIAL_LOGIN" TEXT)

- (int ) insertUserSistem:(int) intIdUsuario
                  strUser: (NSString *) strUser
              strPassword:(NSString *) strPassword
         strSocialLoginId:(NSString *) strSocialLoginId
                 strEmail:(NSString *) strEmail{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_USUARIOS_SISTEMAS (USIS_ID, USIS_US_USUARIO, USIS_US_CLAVE, USIS_ID_SOCIAL_LOGIN, USIS_EMAIL) VALUES(%i, '%@', '%@', '%@', '%@')", intIdUsuario, strUser, strPassword, strSocialLoginId, strEmail];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_USUARIOS_SISTEMAS (USIS_ID, USIS_US_USUARIO, USIS_US_CLAVE, USIS_ID_SOCIAL_LOGIN, USIS_EMAIL)");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
};

- (int ) updateUserSistem:(int) intIdUsuario
                  strUser: (NSString *) strUser
              strPassword:(NSString *) strPassword
         strSocialLoginId:(NSString *) strSocialLoginId
                 strEmail:(NSString *) strEmail{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_USUARIOS_SISTEMAS SET USIS_US_USUARIO = '%@', USIS_US_CLAVE = '%@', USIS_ID_SOCIAL_LOGIN = '%@', USIS_EMAIL ='%@'  WHERE USIS_ID = %i ", strUser, strPassword, strSocialLoginId, strEmail, intIdUsuario];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_USUARIOS_SISTEMAS");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
};

- (int ) updatePasswordUserSistem:(int) intIdUsuario
                      strPassword:(NSString *) strPassword{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return -1;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_USUARIOS_SISTEMAS SET USIS_US_CLAVE = '%@' WHERE USIS_ID = %i ", strPassword, intIdUsuario];
        
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_USUARIOS_SISTEMAS SET USIS_US_CLAVE = WHERE USIS_ID = ");
            return -1;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                return  1;
            }else{
                return -1;
            }
        }
    }
};

- (void ) deleteUserSistem: (int) intIdUsuario{

    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_USUARIOS_SISTEMAS.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
};

- (int ) obtainUserSistem: (int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdUsrRegistered;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID = ");
    }
    
    intIdUsrRegistered = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdUsrRegistered = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdUsrRegistered;
};

- (NSString *) obtainEmailUserSistem:(int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_EMAIL FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_EMAIL FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID =");
    }
    
    NSString *strEmail;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
        strEmail = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return strEmail;

}

/*
 CREATE TABLE "ECOM_USUARIOS_SISTEMAS" (
 "USIS_ID"                  INTEGER PRIMARY KEY NOT NULL,
 "USIS_US_USUARIO"          TEXT NOT NULL,
 "USIS_US_CLAVE"            TEXT NOT NULL,
 "USIS_ID_SOCIAL_LOGIN"     TEXT,
 "USIS_EMAIL"               TEXT)
 */

- (NSArray *) obtainDatosUserSistem:(int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    NSArray * ArrayDatosUserSistem;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID, USIS_EMAIL, USIS_US_CLAVE, USIS_ID_SOCIAL_LOGIN FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID, USIS_EMAIL, USIS_US_CLAVE, USIS_ID_SOCIAL_LOGIN FROM ECOM_USUARIOS_SISTEMAS");
    }
    
    NSString *strIdUser, *strEmail, *strPassword, *strSocialLogin;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
		strIdUser = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
		strEmail = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
		strPassword = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
		strSocialLogin = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
    }
    
    ArrayDatosUserSistem = [NSArray arrayWithObjects: strIdUser, strEmail, strPassword, strSocialLogin, nil];
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return ArrayDatosUserSistem;
}

- (NSString *) obtainCurrentPasswordUserSistem:(int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_US_CLAVE FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_US_CLAVE FROM ECOM_USUARIOS_SISTEMAS WHERE USIS_ID =");
    }
    
    NSString *strPassword;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        
        strPassword = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return strPassword;
    
}

//------------------------------------------------------------------------------------------------

/*
 //Observación: Métodos para saber si el usuario ya se registró localmente con un determinado id de usuario.
 
 - (void ) insertUsrRegistered:(int) intIdLog idUsuario: (int) intIdUsuario;
 
 - (void ) deleteUsrRegistered: (int) intIdUsuario;
 
 - (int ) obtainUsrRegistered: (int) intIdUsuario;
 
 CREATE TABLE "ECOM_REGISTERED" (
 "REG_ID_REGISTERED" INTEGER,
 "USIS_ID" INTEGER
 )
*/

- (void ) insertUsrRegistered:(int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_REGISTERED (USIS_ID) VALUES(%i)", intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_REGISTERED");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteUsrRegistered: (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"DELETE FROM ECOM_REGISTERED WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_REGISTERED");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainUsrRegistered: (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdUsrRegistered;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_REGISTERED WHERE USIS_ID = %i", intIdUsuario];
    const char *sql = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_REGISTERED WHERE USIS_ID = ");
    }
    
    intIdUsrRegistered = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdUsrRegistered = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdUsrRegistered;
}

/*
 - (void ) insertUsrLogged:(int) intIdLog idUsuario: (int) intIdUsuario;
 
 - (void ) deleteUsrLogged: (int) intIdUsuario;
 
 - (int ) obtainUsrLogged: (int) intIdUsuario;

 CREATE TABLE "ECOM_LOGGED" (
 "LOG_ID_LOGGED" INTEGER,
 "SLOG_ID" INTEGER,
 "USIS_ID" INTEGER
 )
 
 1.- Facebook
 2.- Twitter
 3.- Google
 4.- Linked In
*/

 - (void ) insertUsrLogged:(int) intIdLog idUsuario: (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_LOGGED (SLOG_ID, USIS_ID) VALUES(%i, %i)", intIdLog, intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_LOGGED");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteUsrLogged: (int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"DELETE FROM ECOM_LOGGED WHERE USIS_ID = %i", intIdUsuario];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_LOGGED");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainUsrLogged:(int) intIdLog  idUsuario:(int) intIdUsuario{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdUsrLogged;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_LOGGED WHERE USIS_ID = %i AND SLOG_ID = %i", intIdUsuario, intIdLog];

    const char *sentenciaSQL = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_LOGGED WHERE USIS_ID = AND SLOG_ID =");
    }
    
    intIdUsrLogged = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdUsrLogged = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdUsrLogged;
}

- (int ) obtainUsrLoggedIn:(int) intIdUsuario{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdUsrLogged;
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
    }
    NSString *sqlSelect = [NSString stringWithFormat:@"SELECT USIS_ID FROM ECOM_LOGGED WHERE USIS_ID = %i", intIdUsuario];
    
    const char *sentenciaSQL = [sqlSelect UTF8String];
    sqlite3_stmt *sqlStatement;
    
    if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
        NSLog(@"Exception to prepare statement SELECT USIS_ID FROM ECOM_LOGGED WHERE USIS_ID =");
    }
    
    intIdUsrLogged = -1;
    
    while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        intIdUsrLogged = sqlite3_column_int(sqlStatement, 0);
    }
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdUsrLogged;

}

//------------------------------------------------------------------------------------------------
//Métodos para identificar el retorno desde el registro o edición del despacho.
//------------------------------------------------------------------------------------------------

- (void ) insertRegisterBack:(int) intIdRegisterBack{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_BACK_REGISTER (ID_BACK_REGISTER) VALUES('%i')", intIdRegisterBack];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_BACK_REGISTER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteRegisterBack{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"DELETE FROM ECOM_BACK_REGISTER"];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_BACK_REGISTER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainRegisterBack{
    
	NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdRegisterBack;
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT ID_BACK_REGISTER FROM ECOM_BACK_REGISTER";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT ID_BACK_REGISTER FROM ECOM_BACK_REGISTER");
	}
    
    intIdRegisterBack = -1;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		intIdRegisterBack = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdRegisterBack;
}

//------------------------------------------------------------------------------------------------------
//CREATE TABLE "ECOM_SEARCH_BANNER"   ("SEARCH_BANNER_ID" INTEGER)
//CREATE TABLE "ECOM_SELECTED_BANNER" ("SBAN_SELECTED_ID" INTEGER)
/*
- (void ) insertSearchBanner:(int) intIdSearchBanner;

- (void ) deleteSearchBanner;

- (int ) obtainSearchBanner;
//------------------------------------------------------------------------------------------------------

- (void ) insertSelectedBanner:(int) intIdSelectedBanner;

- (void ) deleteSelectedBanner;

- (int ) obtainSelectedBanner;
*/
//------------------------------------------------------------------------------------------------------

- (void ) insertSelectedBanner:(int) intIdSelectedBanner{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_SELECTED_BANNER (SBAN_SELECTED_ID) VALUES('%i')", intIdSelectedBanner];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_SELECTED_BANNER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteSelectedBanner{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"DELETE FROM ECOM_SELECTED_BANNER"];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_SELECTED_BANNER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainSelectedBanner{
    
	NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdSelectedBanner;
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT SBAN_SELECTED_ID FROM ECOM_SELECTED_BANNER";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT SBAN_SELECTED_ID FROM ECOM_SELECTED_BANNER");
	}
    
    intIdSelectedBanner = -1;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		intIdSelectedBanner = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdSelectedBanner;
}

- (void ) insertSearchBanner:(int) intIdSearchBanner{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_SEARCH_BANNER (SEARCH_BANNER_ID) VALUES('%i')", intIdSearchBanner];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_SEARCH_BANNER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteSearchBanner{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"DELETE FROM ECOM_SEARCH_BANNER"];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_SEARCH_BANNER");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainSearchBanner{
    
	NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdSearchBanner;
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT SEARCH_BANNER_ID FROM ECOM_SEARCH_BANNER";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT SEARCH_BANNER_ID FROM ECOM_SEARCH_BANNER");
	}
    
    intIdSearchBanner = -1;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		intIdSearchBanner = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdSearchBanner;
}

//------------------------------------------------------------------------------------------------------

- (void ) insertProductBack:(int) intIdSearchBack{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_BACK_PRODUCT (ID_BACK_PRODUCT) VALUES('%i')", intIdSearchBack];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_BACK_PRODUCT");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) insertSearchBack:(int) intIdSearchBack{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_BACK_SEARCH (ID_BACK_SEARCH) VALUES('%i')", intIdSearchBack];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_BACK_SEARCH");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) updateProductBack:(int) intIdSearchBack{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_BACK_PRODUCT SET ID_BACK_PRODUCT ='%i' ", intIdSearchBack];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_BACK_SEARCH SET ID_BACK_SEARCH");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) deleteProductBack{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"DELETE FROM ECOM_BACK_PRODUCT"];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_BACK_PRODUCT");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void ) updateSearchBack:(int) intIdSearchBack{

    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"UPDATE ECOM_BACK_SEARCH SET ID_BACK_SEARCH ='%i' ", intIdSearchBack];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement UPDATE ECOM_BACK_SEARCH SET ID_BACK_SEARCH");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) obtainProductBack{

	NSString *ubicacionDB = [self obtenerRutaBD];
    
    int intIdSearchBack;
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT ID_BACK_PRODUCT FROM ECOM_BACK_PRODUCT";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT ID_BACK_PRODUCT FROM ECOM_BACK_PRODUCT");
	}
    
    intIdSearchBack = -1;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		intIdSearchBack = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdSearchBack;
}

- (int ) obtainSearchBack{
    
	NSString *ubicacionDB = [self obtenerRutaBD];

    int intIdSearchBack;
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT ID_BACK_SEARCH FROM ECOM_BACK_SEARCH";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT ID_BACK_SEARCH FROM ECOM_BACK_SEARCH");
	}
    
    intIdSearchBack = 0;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		intIdSearchBack = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return intIdSearchBack;
}
//----------------------------------------------------------------------------------------------------------------------------------------------
//********CHAT**********************************************

- (void) crearTextWritingChat: (NSString *) strTextChat {
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_WRITING (MESSAGE) VALUES ('%@')", strTextChat];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO ECOM_WRITING");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (void) borrarTextWritingChat{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_WRITING"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_WRITING.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (NSString*) obtenerTextWritingChat{
    
    NSString *strTextWriting;
    
	NSString *ubicacionDB = [self obtenerRutaBD];
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
	const char *sentenciaSQL = "SELECT MESSAGE FROM ECOM_WRITING";
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT MESSAGE FROM ECOM_WRITING");
	}
    
    strTextWriting = @"";
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
        strTextWriting = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return strTextWriting;
}

- (int ) obtenerIdMaxPrivateChat: (NSInteger) idU{
    
    int idMaxPrivateChat;
	NSString *ubicacionDB = [self obtenerRutaBD];
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
    NSString *strSql = [NSString stringWithFormat:@"SELECT MAX(idp) FROM ECOM_CHATS WHERE idUsr =%li", (long)idU];
    const char *sentenciaSQL = [strSql UTF8String];
    
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT MAX(idp) FROM ECOM_CHATS WHERE idUsr =");
	}
    
    idMaxPrivateChat = 0;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		idMaxPrivateChat = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return idMaxPrivateChat;
    
}

- (void) insertIdPrivateChat: (NSString *) idUsr
                         idp: (NSString *) idp
                         idU: (NSString *) idU
                           m: (NSString *) m
                           r: (NSString *) r
                           w: (NSString *) w
                           t: (NSString *) t
                           u: (NSString *) u
                           i: (NSString *) i
                          re: (NSString *) re
                           p: (NSString *) p{
    
    NSString *ubicacionDB = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO ECOM_CHATS (idUsr,idp,idU,m,r,w,t,u,i,re,p ) VALUES ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')", idUsr, idp,idU,m,r,w,t,u,i,re,p];
        const char *sql = [sqlInsert UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement INSERT INTO IRN_CHATS");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
    
}

- (void ) borraridPrivateChat: (NSInteger) idPrivateChat{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CHATS  WHERE idp =%li", (long)idPrivateChat];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE ECOM_CHATS WHERE idp =%li", (long)idPrivateChat);
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
    
}

- (void) borrarChats{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CHATS"];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE ECOM_CHATS.");
            return;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
            }
        }
    }
}

- (int ) existeIdPrivateChat:(NSInteger) idp{
    
    int idPrivateChat;
	NSString *ubicacionDB = [self obtenerRutaBD];
    
	if(!(sqlite3_open([ubicacionDB UTF8String], &bd) == SQLITE_OK)){
		NSLog(@"Database connection failure irn");
	}
    
    NSString *strSql = [NSString stringWithFormat:@"SELECT idp FROM ECOM_CHATS WHERE idp =%li", (long)idp];
    const char *sentenciaSQL = [strSql UTF8String];
    
	sqlite3_stmt *sqlStatement;
    
	if(sqlite3_prepare_v2(bd, sentenciaSQL, -1, &sqlStatement, NULL) != SQLITE_OK){
		NSLog(@"Exception to prepare statement SELECT idp FROM ECOM_CHATS WHERE idp =");
	}
    
    idPrivateChat = 0;
    
	while(sqlite3_step(sqlStatement) == SQLITE_ROW){
		idPrivateChat = sqlite3_column_int(sqlStatement, 0);
	}
    
    sqlite3_finalize(sqlStatement);
    sqlite3_close(bd);
    
    return idPrivateChat;
}

- ( int ) borrarChatsxIdUser: (NSInteger) idUsrChat{
    
    NSString *ubicacionBD = [self obtenerRutaBD];
    int deleteSucessfull = 0;
    
    if(!(sqlite3_open([ubicacionBD UTF8String], &bd) == SQLITE_OK)){
        NSLog(@"Database connection failure irn");
        return deleteSucessfull;
    } else {
        NSString *sqlDelete = [NSString stringWithFormat:@"DELETE FROM ECOM_CHATS WHERE idUsr=%li", (long)idUsrChat];
        const char *sql = [sqlDelete UTF8String];
        sqlite3_stmt *sqlStatement;
        
        if(sqlite3_prepare_v2(bd, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Exception to prepare statement DELETE FROM ECOM_CHATS WHERE idUsr=");
            return deleteSucessfull;
        } else {
            if(sqlite3_step(sqlStatement) == SQLITE_DONE){
                sqlite3_finalize(sqlStatement);
                sqlite3_close(bd);
                deleteSucessfull = 1;
            }
        }
    }
    return deleteSucessfull;
}





@end
